<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<t:link />

<title>角色管理</title>
</head>



<body>

	<div class="manager">
		<div>角色列表</div>
		<button id="button" type="button" class="btn btn-default add">新增角色</button>
	</div>
	<div>
		<table class="table table-hover" id="cusTable" data-pagination="true"
			data-show-refresh="true" data-show-toggle="true"
			data-showColumns="true">
			<thead>
				<tr>
					<%-- <th data-checkbox="true"></th> --%>
					<th data-field="id" class="tc">ID</th>
					<th data-field="name" class="tc">角色名称</th>
					<th data-field="memo" class="tc">角色描述</th>
					<%-- <th data-field="createDate" class="tc"
						data-formatter="dataFormatter" >创建日期</th> --%>
					<th class="col-xs-2" data-field="action"
						data-formatter="actionFormatter" data-events="actionEvents">操作</th>
				</tr>
			</thead>
		</table>
	</div>


	<div id="dlg" style="display: none">
		<div id="tb" style="padding: 3px">
			<span>角色编码:</span> <input id="name"
				style="line-height: 26px; border: 1px solid #ccc"> <span>角色名称</span>
			<input id="memo" style="line-height: 26px; border: 1px solid #ccc">
		</div>
		<div id="datagrid"></div>
	</div>

</body>
<script type="text/javascript">

	$(document).ready(function() {
		//调用函数，初始化表格  
		initTable();
	});

	function initTable() {
		//先销毁表格  
		$('#cusTable').bootstrapTable('destroy');
		//初始化表格,动态从服务器加载数据  
		$("#cusTable").bootstrapTable({
			method : "POST", //使用get请求到服务器获取数据  
			url : "${pageContext.request.contextPath}/role/roledata",//获取数据的Servlet地址  
			striped : true, //表格显示条纹  
			pagination : true, //启动分页  
			pageSize : 5, //每页显示的记录数  
			pageNumber : 1, //当前第几页  
			pageList : [ 5, 10, 15, 20, 25 ], //记录数可选列表  
			search : true, //是否启用查询  
			showColumns : true, //显示下拉框勾选要显示的列  
			showRefresh : true, //显示刷新按钮  
			sidePagination : "client", //表示客户端分页 
			/* columns: [{
			    checkbox: true
			}], */
			onLoadSuccess : function(data) { //加载成功时执行  
				//alert("加载成功!" + data);
			},
			onLoadError : function(data) { //加载失败时执行  
				//alert("加载失败!" + data);
			}
		});
	}

	function actionFormatter(val, row, index) {
		return '<a class="mod tc" >修改</a> ' + '<a class="delete tc" >删除</a>'
				+ '<a class="permis tc" >分配权限</a>';
	}

	var roleid = '';
	window.actionEvents = {
		'click .mod' : function(e, value, row, index) {
			window.open("${pageContext.request.contextPath}/role/update?id="
					+ row.id, "_self");
		},
		'click .permis' : function(e, value, row, index) {
			$('#dlg').dialog({
				title : '分配权限',
				width : 600,
				height : 450
			});
			roleid = row.id;
			searchData();

		},
		'click .delete' : function(e, value, row, index) {
			if (row) {
				$.ajax({
					type : 'POST',
					url : '${pageContext.request.contextPath}/role/roledelete',
					data : {
						id : row.id
					},
					success : function(data) {
						if (data) {
							alert("删除成功!");
						}
					},
					error : function() {
						alert("删除失败!");
					}
				})
			}
		}
	}

	function searchData() {
		//bootstrap列表复选框获取选中行数据
		//var a= $('#cusTable').bootstrapTable('getSelections');  
		//alert(a[0].name);
		var map = {
			name : $("#name").val(),
			memo : $("#memo").val()
		};

		$("#datagrid")
				.datagrid(
						{
							url : '${pageContext.request.contextPath}/permission/permissiondatalist',
							queryParams : {
								map : JSON.stringify(map)
							},
							rownumbers : true,
							title : "权限列表",
							contentType : "charset=UTF-8",
							pagination : true,
							idField : 'id',
							pageSize : 10,
							pageList : [ 1, 2, 5, 10, 15, 20 ],
							columns : [ [ {
								field : 'id',
								title : '权限ID',
								checkbox : true,
							}, {
								field : 'name',
								title : '权限编码',
							}, {
								field : 'memo',
								title : '权限名称',
							} ] ],
							toolbar : [ {
								text : "保存",
								iconCls : "icon-save",
								handler : function() {
									obj.save();
								}
							}, '-', {
								text : '查询',
								iconCls : 'icon-search',
								handler : function() {
									obj.search();
								}
							} ]
						})
	}

	obj = {

		search : function() {
			searchData();
		},

		save : function() {
			var ids = [];
			//获取行数据
			var rows = $('#datagrid').datagrid('getSelections');
			for (var i = 0; i < rows.length; i++) {
				ids.push(rows[i].id);
			}
			$
					.ajax({
						type : 'POST',
						url : '${pageContext.request.contextPath}/permission/rolepermis',
						data : {
							roleid : roleid,
							ids : JSON.stringify(ids)
						},
						success : function(data) {
							if (data) {
								alert("保存成功!");
							}
						},
						error : function() {
							alert("保存失败!");
						}
					})

		}
	}

	$("#button").click(
			function() {
				window.open("${pageContext.request.contextPath}/role/roleadd",
						"_self");
			})
</script>






</html>